package etxt2db.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import etxt2db.api.ClassificationExecutor;
import etxt2db.api.ClassificationModel;
import etxt2db.serialization.ClassificationModelSerializer;

public class Tester {

	public class TesterRunnable implements Runnable{

		private int split;
		private String name;

		public TesterRunnable(int split, String name){
			this.split = split;
			this.name = name;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
		
			ClassificationModelSerializer serial = new ClassificationModelSerializer();

			List<String> types = new ArrayList<String>();
			
			types.add("NATURALDISASTER");

			String testingDirectory = "/home/pjbarrio/Dataset/NaturalDisaster/SimpleData/" + split + "/testingDirectory/";
			
			File testingFile = new File(testingDirectory);
			
			String classifiedDirectory = testingDirectory + "results" + name + "/";
			
			try {
				System.setOut(new PrintStream(new File(classifiedDirectory + "output.testing")));
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			File classifiedFile = new File(classifiedDirectory);
			
			classifiedFile.mkdir();
			
			ClassificationModel model = serial.deserializeClassificationModel("/home/pjbarrio/Dataset/NaturalDisaster/SimpleData/" + split + "/NaturalDisaster" + name + ".bin");
			
			ClassificationExecutor exec = new ClassificationExecutor();

			File[] files = testingFile.listFiles();
			
			try {
			
				for (File file : files) {
					
					if (file.getName().endsWith(".txt")){
						
						System.out.println(file);
						
						exec.createClassifiedFile(file, model, types, classifiedDirectory);

					}
					
				}
				
				System.out.println(exec.getClassifiedSegments(testingFile, model, types));

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
		}
		
	}
	
	/**
	 * @param args
	 * @throws ParseException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, ParseException {
		
		int[] split = {0,1,2,3,4,5,6,7,8,9};
		
		String[] names = {"HMM","MEMM","CRF","SVM"}; 
		
		for (int i = 0; i < names.length; i++) {
			
			String name = names[i];
			
			for (int j = 0; j < split.length; j++) {
				
				int spl = split[j];
				
				new Tester().execute(name,spl);
				
			}
			
		}

	}

	private void execute(String name, int spl) {
		
		System.out.println(name + " - " + spl);
		
		Thread t = new Thread(new TesterRunnable(spl, name));
		
		t.start();
		
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
